//package com.group.app.example.es.impl;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//
//import com.group.app.example.es.ElasticsearchHandler;
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.bulk.BulkItemResponse;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.support.WriteRequest;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Repository;
//import org.springframework.util.CollectionUtils;
//import org.springframework.util.StringUtils;
//
//import java.io.IOException;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
////@Repository
//public class ElasticsearchHandlerImpl implements ElasticsearchHandler {
//
//    @Autowired
//    private RestHighLevelClient restHighLevelClient;
//
//    @Override
//    public boolean upserDocuments(String index, JSONArray docs, String id) {
//        BulkRequest request=new BulkRequest();
//        for(int i=0;i<docs.size();i++){
//            request.add(new IndexRequest(index)
//                    .id(((JSONObject)docs.get(i)).getString(id))
//                    .source(docs.getString(i), XContentType.JSON));
//        }
//        return bulk(request);
//    }
//
//    @Override
//    public boolean updateDocument(String index, JSONArray docs, String id) {
//        BulkRequest request=new BulkRequest();
//        for(int i=0;i<docs.size();i++){
//            request.add(new UpdateRequest(index,
//                    ((JSONObject)docs.get(i)).getString(id)).doc(docs.getString(i),
//                    XContentType.JSON));
//
//        }
//        return bulk(request);
//    }
//
//    @Override
//    public boolean deleteDocuments(String index, List<String> id) {
//        BulkRequest bulkRequest =new BulkRequest();
//        id.forEach(i->bulkRequest.add(new DeleteRequest(index,i)));
//        return bulk(bulkRequest);
//    }
//
//    @Override
//    public boolean deleteDocument(String index, String id) {
//        BulkRequest bulkRequest =new BulkRequest();
//        bulkRequest.add(new DeleteRequest(index,id));
//        return bulk(bulkRequest);
//    }
//
//    @Override
//    public boolean deleteIndex(String index) {
//        DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest(index);
//        boolean acknowledged=false;
//        try{
//            acknowledged=restHighLevelClient.indices()
//                    .delete(deleteIndexRequest,RequestOptions.DEFAULT)
//                    .isAcknowledged();
//        }catch (IOException e){
//            e.printStackTrace();
//        }
//        return  acknowledged;
//    }
//
//    @Override
//    public SearchSourceBuilder getSourceBuilder(int size, int from) {
//        SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//        if(size>=0&&size<=1000000){
//            sourceBuilder.size(size);
//        }
//        if (from>=0){
//            sourceBuilder.from(from);
//        }
//        sourceBuilder.trackTotalHits(true);
//        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//        return sourceBuilder;
//    }
//
//    @Override
//    public SearchSourceBuilder getSourceBuilder(int size) {
//
//        return getSourceBuilder(size,-1);
//    }
//
//    @Override
//    public BoolQueryBuilder getQueryBuilder(String beginDate, String endDate, List<String> channel, List<String> business) {
//        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
//        boolQueryBuilder.filter(QueryBuilders.rangeQuery("changeDate")
//        .gte(beginDate)
//        .lte(endDate)
//        .timeZone("+08:00")
//        .format("yyyyMMdd"));
//        if(!CollectionUtils.isEmpty(channel)){
//            boolQueryBuilder.filter(QueryBuilders.termQuery("channel.keyword",channel));
//        }
//        if(!CollectionUtils.isEmpty(business)){
//            boolQueryBuilder.filter(QueryBuilders.termQuery("business.keyword",business));
//        }
//        return boolQueryBuilder;
//    }
//
//    @Override
//    public HighlightBuilder getHighlightBuilder(List<String> fieldList) {
//        HighlightBuilder highlightBuilder=new HighlightBuilder();
//        highlightBuilder.preTags("<span style='color:red;'>");
//        highlightBuilder.postTags("</span>");
//        fieldList.forEach(i->highlightBuilder.field(i));
//        return highlightBuilder;
//    }
//
//    @Override
//    public SearchResponse getSearchResponse(String index, SearchSourceBuilder sourceBuilder) {
//        return getSearchResponse(index,sourceBuilder,null,null);
//    }
//
//    @Override
//    public SearchResponse getSearchResponse(String index, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder) {
//        return getSearchResponse(index,sourceBuilder,queryBuilder,null);
//    }
//
//    @Override
//    public SearchResponse getSearchResponse(String index, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder, HighlightBuilder highlightBuilder) {
//        return getSearchResponse(index,sourceBuilder,queryBuilder,highlightBuilder);
//    }
//
//    @Override
//    public SearchResponse getSearchResponse(String index, SearchSourceBuilder sourceBuilder, QueryBuilder queryBuilder, HighlightBuilder highlightBuilder, List<String> includeList, List<String> excludeList) {
//        String[] includes,excludes;
//        includes=includeList!=null?includeList.toArray(new String[includeList.size()]):null;
//        if (excludeList !=null){
//            excludeList.add("id");
//            excludes=excludeList.toArray(new String[excludeList.size()]);
//        }else{
//            excludes=new String[]{"id"};
//        }
//        sourceBuilder.fetchSource(includes,excludes);
//        if(queryBuilder!=null){
//            sourceBuilder.query(queryBuilder);
//        }
//        if(highlightBuilder !=null){
//            sourceBuilder.highlighter(highlightBuilder);
//        }
//        SearchRequest searchRequest=new SearchRequest(index);
//        searchRequest.source(sourceBuilder);
//        SearchResponse response=null;
//        try{
//            response=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
//        }catch (IOException e){
//            e.printStackTrace();
//        }
//        return response;
//    }
//
//    @Override
//    public BoolQueryBuilder getQueryBuilderByKeyword(String beginDate, String endDate, List<String> channel, List<String> business, String keyword) {
//        BoolQueryBuilder boolQueryBuilder=getQueryBuilder(beginDate,endDate,channel,business);
//        if (StringUtils.isEmpty(keyword)){
//            boolQueryBuilder.must(QueryBuilders.multiMatchQuery(keyword).type("best_fields"));
//        }
//        return boolQueryBuilder;
//    }
//
//    /**
//     * 批量提交插入更新删除文档请求
//     * @param request
//     * @return 是否失败
//     */
//    private boolean bulk(BulkRequest request){
//        request.timeout(TimeValue.timeValueSeconds(30));
//        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
//        BulkResponse bulkResponse=null;
//        try {
//            bulkResponse = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        for (BulkItemResponse response:bulkResponse){
//            if (response.isFailed()){
//                BulkItemResponse.Failure failure=response.getFailure();
//                System.out.println(failure.getMessage());
//            }
//        }
//        return !bulkResponse.hasFailures();
//    }
//}
